﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Main_Reports_Accounts_AccountBooks_DayBook : System.Web.UI.Page
{
    Datautility du = new Datautility();
    AccountSettings ac = new AccountSettings();
    double dbldebit = 0;
    double dblcredit = 0;
    string previousrow = string.Empty;
    int firstrow = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            lblcompname.Text = Session["CompName"].ToString();
            hidFinyear.Value = Session["FinYear"].ToString();
            txtFromDate.Text = "01/04/" + Session["FinYear"].ToString();
            txtToDate.Text = "31/03/" + (Convert.ToInt32(Session["FinYear"].ToString()) + 1).ToString();
            DataTable dt = getDayBookDetails(txtFromDate.Text, txtToDate.Text);
            DataView dv = dt.DefaultView;

            string[] strcolumn = { "TransactionDate" };

            DataTable dt1 = dv.ToTable(true, strcolumn);
            Datalist1.DataSource = dt1;
            Datalist1.DataBind();

        }

    }
    protected DataTable getDayBookDetails(string strFromDate, string strToDate)
    {
        DataTable dt = new DataTable();

        if (strFromDate != string.Empty && strToDate != string.Empty)
        {
            string[] str = strFromDate.Split('/');
            strFromDate = str[1] + "/" + str[0] + "/" + str[2];
            str = strToDate.Split('/');
            strToDate = str[1] + "/" + str[0] + "/" + str[2];

            SqlParameter[] pr ={new SqlParameter ("@FromDate",strFromDate),
                            new  SqlParameter("@ToDate",strToDate),
                            new  SqlParameter("TransactionTypeList", "1,2,3,4,6,7,8,9,10,11")
                           };

            dt = du.getdatausingprocedure("USP_ACC_AccountBookVoucherDetails", pr);
            lblheading.Text = "Day Book From " + string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(strFromDate)) + " To " + string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(strToDate));

            if (dt.Rows.Count == 0)
            {
                panel1.Visible = true;
                Datalist1.Visible = false;
            }
            else
            {
                panel1.Visible = false;
                Datalist1.Visible = true;
            }
        }
        return dt;

    }
    protected void Datalist_itembound(object sender, DataListItemEventArgs e)
    {
        firstrow = 0;
        previousrow = string.Empty;
        ImageButton img = (ImageButton)e.Item.FindControl("imgShow");
        GridView grv = (GridView)e.Item.FindControl("grdDisplay");
        Label lb = (Label)e.Item.FindControl("lblDate");
        DataTable dt = getDayBookDetails(txtFromDate.Text, txtToDate.Text);
        DataView dv = dt.DefaultView;
        dv.RowFilter = "TransactionDate=#" + Convert.ToDateTime(lb.Text.ToString()) + "#";
        DataTable dt1 = dv.ToTable();
        grv.DataSource = dt1;
        grv.DataBind();

        img.Attributes.Add("onclick", "return expandDetailsInGrid('" + img.ClientID + "','" + grv.ClientID + "');");
    }

    protected void btnShow_Click(object sender, EventArgs e)
    {
        DataTable dt = getDayBookDetails(txtFromDate.Text, txtToDate.Text);


        Datalist1.DataSource = dt;
        Datalist1.DataBind();


    }
    protected void row_DataBound(object sender, GridViewRowEventArgs e)
    {
        GridView grv = (GridView)sender;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[5].Text != string.Empty)
            {
                e.Row.Cells[5].Text = Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text)).ToString();
            }
            if (Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text)) != 0)
            {
                e.Row.Cells[5].Text = ac.GetCurrency((Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text)))).ToString();
            }
            else
            {
                e.Row.Cells[5].Text = string.Empty;

            }

            if (e.Row.Cells[6].Text != string.Empty)
            {
                e.Row.Cells[6].Text = Math.Abs(Convert.ToDouble(e.Row.Cells[6].Text)).ToString();
            }
            if (Math.Abs(Convert.ToDouble(e.Row.Cells[6].Text)) != 0)
            {
                e.Row.Cells[6].Text = ac.GetCurrency((Math.Abs(Convert.ToDouble(e.Row.Cells[6].Text)))).ToString();
            }
            else
            {
                e.Row.Cells[6].Text = string.Empty;

            }
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            DataRowView dv = (DataRowView)e.Row.DataItem;

            if (previousrow == dv["TransactionNumber"].ToString())
            {
                if (grv.Rows[firstrow].Cells[3].RowSpan == 0)
                {
                    grv.Rows[firstrow].Cells[3].RowSpan = 2;
                }
                else
                {
                    grv.Rows[firstrow].Cells[3].RowSpan += 1;
                }
                if (grv.Rows[firstrow].Cells[2].RowSpan == 0)
                {
                    grv.Rows[firstrow].Cells[2].RowSpan = 2;
                }
                else
                {
                    grv.Rows[firstrow].Cells[2].RowSpan += 1;
                } 
                if (grv.Rows[firstrow].Cells[1].RowSpan == 0)
                {
                    grv.Rows[firstrow].Cells[1].RowSpan = 2;
                }
                else
                {
                    grv.Rows[firstrow].Cells[1].RowSpan += 1;
                }

                if (grv.Rows[firstrow].Cells[0].RowSpan == 0)
                {
                    grv.Rows[firstrow].Cells[0].RowSpan = 2;
                }
                else
                {
                    grv.Rows[firstrow].Cells[0].RowSpan += 1;
                }
               

                e.Row.Cells.RemoveAt(3);
                e.Row.Cells.RemoveAt(2);
                e.Row.Cells.RemoveAt(1);
                e.Row.Cells.RemoveAt(0);
            }
               
        
            else
            {
                e.Row.VerticalAlign = VerticalAlign.Middle;
                firstrow = e.Row.RowIndex;
                previousrow = dv["TransactionNumber"].ToString();
            }
            e.Row.Cells[2].Attributes.Add("border-right", "solid 1px #818181");
        }
    }
}
